package com.yangoxix.lovegoing.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yangoxix.lovegoing.pojo.AuditActivity;
import com.yangoxix.lovegoing.pojo.AuditPublisher;
import com.yangoxix.lovegoing.pojo.AuditUser;
import com.yangoxix.lovegoing.service.AuditService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 审核的controller
 *
 * @author chenzou
 * @date 2021 -09-18 10:37:50
 */
@Controller
@RequestMapping("Audit")
@Api(tags="审核的接口")
public class AuditController {

    /**
     * The Auditsuccess.
     */
    static String AUDITSUCCESS = "success";
    /**
     * The Auditfail.
     */
    static String AUDITFAIL = "fail";

    @Autowired
    private AuditService service;


    /**
     * Audit activity by name.审批活动
     *
     * @param name   活动名
     * @param result 审核的结果
     * @return the response entity
     * @author chenzou
     * @date 2021 -09-18 10:38:15
     */
    @GetMapping("AuditAct")
    @ResponseBody
    public ResponseEntity auditActivityByName(String name, String result) {

        if (result.equals(AUDITSUCCESS)) {
            return new ResponseEntity(service.auditPass(name),HttpStatus.OK);
        } else if (result.equals(AUDITFAIL)) {
            return new ResponseEntity(service.auditFail(name),HttpStatus.OK);
        }else {
            return new ResponseEntity("请求参数有误",HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * Get all audit act response entity.获取全部待审批的活动
     *
     * @return the response entity
     */
    @ApiOperation("获取全部待审批的活动")
    @GetMapping("getAllAuditAct")
    @ResponseBody
    public ResponseEntity getAllAuditAct(){
        return new ResponseEntity(service.getAllAuditAct(),HttpStatus.OK);
    }

    /**
     * 报名活动
     *
     * @param activityName the activity name
     * @param userName     the user name
     * @return the response entity
     * @author chenzou
     * @date 2021 -09-18 10:38:15
     */
    @GetMapping("applyAct")
    @ResponseBody
    public ResponseEntity auditUsernameByName(String activityName, String userName) {

        String s = service.activityJoinUser(activityName, userName);
        return s==null? new ResponseEntity(
                "报名成功",
                HttpStatus.OK):new ResponseEntity(
                s,
                HttpStatus.BAD_REQUEST);


    }

    /**
     * Audit pass user.对用户审批
     *
     * @param name         the name
     * @param activityname the activityname
     * @param result       审核的结果
     * @author chenzou
     * @date 2021 -09-18 10:38:15
     */
    @GetMapping("auditUser")
    @ResponseBody
    public void auditPassUser(String name, String activityname, String result) {
        if (result.equals(AUDITSUCCESS)) {
            service.auditPassUser(name, activityname);
        } else if (result.equals(AUDITFAIL)) {
//          审核不通过，需要写一个通知的接口，放回审核失败的信息
            service.auditUnpassUser(name, activityname);
        }
    }


    /**
     * Upload activity response entity.上传活动
     *
     * @param formJson 前端传来的对象json字符串
     * @return the response entity
     * @author chenzou
     * @date 2021 -09-18 10:38:15
     */
    @GetMapping("uploadAct")
    @ResponseBody
    public ResponseEntity<String> uploadActivity(String formJson) {
        JSONObject jsonObject = JSONObject.parseObject(formJson);
        AuditActivity auditActivity = JSONObject.toJavaObject(jsonObject, AuditActivity.class);
//        判断数据库中是否有数据
        QueryWrapper<AuditActivity> wrapper = new QueryWrapper<>();
        wrapper.eq("activityname", auditActivity.getActivityname());
        if (auditActivity.selectCount(wrapper) != 0) {
            return new ResponseEntity<>(
                    "已经有这个活动名了，建议换一个",
                    HttpStatus.BAD_REQUEST);
        }
        auditActivity.insert();
        return new ResponseEntity<>(
                "已提交审核",
                HttpStatus.OK);
    }

    /**
     * Get all publisher applyed response entity.获取全部待审批的活动发布者
     *
     * @return the response entity
     */
    @ApiOperation("获取全部待审批的活动发布者")
    @GetMapping("getAllAuditPublisherApplyed")
    @ResponseBody
    public ResponseEntity getAllPublisherApplyed(){
        List<AuditPublisher> allPublisherApplyed = service.getAllPublisherApplyed();
        if (allPublisherApplyed != null) {
            return new ResponseEntity(allPublisherApplyed, HttpStatus.OK);
        }
        return null;
    }
}
